< previous page page_136 next page >

Page 136
FOF_WANTMAPPINGHANDLE sets the hNameMappings field to refer to an array of SHNAMEMAPPING structures containing the source and destination file names after the operation.
What makes this particular function interesting is the format of these strings. How does one string specify multiple files? Unlike Visual Basic, where the string length is kept internally for each string, Win32 API functions use the C convention, where a string is terminated by a NULL character. That's why Win32 API strings cannot contain embedded NULLs, whereas Visual Basic strings can. This function uses a common convention for passing multiple strings in a buffer, where each string is separated by a NULL character and the final string is terminated by two NULL characters.
The FileOp1 sample program contains two routines to convert string arrays to and from these double NULL-terminated string buffers. GetNullsString converts an array of strings into a buffer and is shown below:
Private Function GetNullsString(files() As String) As String
   Dim x&
   Dim res$
   For x = 1 To UBound(files)
      res$ = res$ & files(x) & Chr$(0)
   Next x
   ' Why do we add two nulls here? Can you guess?
   res$ = res$ & Chr$(0) & Chr$(0)
   GetNullsString = res$
End Function
To answer the question in the listing, consider what happens if there are no strings in the array. The reverse conversion is more complex and is shown in the GetFileArray function here:
' Convert a double null-terminated string
Private Function GetFileArray(NullsString As String, Optional _
ByVal SeparatorChar As Byte = 0) As Variant
   Dim nullpos&
   Dim curpos&
   Dim results() As String
   Dim itemcount&
   curpos = 1 ' Start location
   ReDim results(O)

   Do
      nullpos = InStr(curpos, NullsString, Chr$(SeparatorChar))

 
< previous page page_136 next page >